package com.ruoyi.web.controller.miniprogram;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 小程序用户控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/miniprogram/user")
public class MiniprogramUserController extends BaseController
{
    /**
     * 获取当前用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("/info")
    public AjaxResult getUserInfo()
    {
        try
        {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser == null)
            {
                return AjaxResult.error("用户未登录");
            }
            
            AjaxResult ajax = AjaxResult.success();
            ajax.put("userId", loginUser.getUserId());
            ajax.put("username", loginUser.getUsername());
            ajax.put("nickName", loginUser.getUser().getNickName());
            ajax.put("avatar", loginUser.getUser().getAvatar());
            ajax.put("sex", loginUser.getUser().getSex());
            ajax.put("permissions", loginUser.getPermissions());
            
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error("获取用户信息失败：" + e.getMessage());
        }
    }
}
